using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manager_users_view_user : System.Web.UI.Page
{
    private string id = "";
    string user_present = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        // test user login yet
        user_present = (string)Session["user"];
        if (user_present == null)
        {
            Response.Redirect("../main/login.aspx");
        }

        id = Request["id"];
        if (id == "new")
        {
            button_save.ImageUrl = "../images/create.gif";
        }
        else
        {
            button_save.ImageUrl = "../images/update.gif";
            if (Page.IsPostBack == false)
            {
                BindData();
            }
        }

    }

    protected void BindData()
    {
        Users user = Users.get_user(Convert.ToInt32(id));
        text_email.Text = user.Email;
        text_firstname.Text = user.FirstName;
        text_lastname.Text = user.LastName;
        text_mobiphone.Text = user.MobiPhone;
        text_username.Text = user.UserName;
    }

    protected void execute(object sender,EventArgs e)    
    {
        if (id == "new")
        {
            try
            {
                Users.create_user(text_username.Text, text_password.Text, text_email.Text, text_firstname.Text, text_lastname.Text, text_mobiphone.Text, true, user_present);
                Response.Redirect("list_user.aspx");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
        else
        {
            try
            {
                Users.update_user(Convert.ToInt32(id), text_username.Text, text_password.Text, text_email.Text, text_firstname.Text, text_lastname.Text, text_mobiphone.Text, true, user_present);
                Response.Redirect("list_user.aspx");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(),"Message","<script language='javascript'>alert('"+ex.Message.ToString().Replace("'","")+"');</script>");
            }
        }
    }

}
